package com.syy.api.system.impl;

import cn.hutool.core.collection.CollUtil;
import com.syy.api.system.mapper.RoleMapper;
import com.syy.api.system.service.RoleService;
import com.syy.common.core.domain.entity.SysRole;
import com.syy.common.enums.CommonStatusEnum;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static com.syy.common.enums.ErrorCodeConstants.ROLE_IS_DISABLE;
import static com.syy.common.enums.ErrorCodeConstants.ROLE_NOT_EXISTS;
import static com.syy.common.exception.ServiceExceptionUtil.exception;
import static com.syy.system.sms.CollectionUtils.convertMap;

public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Override
    public void validateRoleList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        // 获得角色信息
        List<SysRole> roles = roleMapper.selectBatchIds(ids);
        Map<Long, SysRole> roleMap = convertMap(roles, SysRole::getRoleId);
        // 校验
        ids.forEach(id -> {
            SysRole role = roleMap.get(id);
            if (role == null) {
                throw exception(ROLE_NOT_EXISTS);
            }
            if (!CommonStatusEnum.ENABLE.getStatus().equals(role.getStatus())) {
                throw exception(ROLE_IS_DISABLE, role.getRoleName());
            }
        });
    }
}
